package com.yonyou.lxp.xutils3test;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.yonyou.lxp.xutils3test.ui.BaseActivity;
import com.yonyou.lxp.xutils3test.ui.bitmap.BitmapFragment;
import com.yonyou.lxp.xutils3test.ui.db.DbFragment;
import com.yonyou.lxp.xutils3test.ui.http.HttpFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;


@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.container)
    private ViewPager mViewPager;

    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;

    @ViewInject(R.id.tabs)
    private TabLayout tabLayout;

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private Fragment dbFragment;
    private Fragment httpFragment;
    private Fragment bitmapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        tabLayout.setupWithViewPager(mViewPager);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (httpFragment == null) {
                        httpFragment = HttpFragment.newInstance("1", "");
                    }
                    return httpFragment;

                case 1:
                    if (dbFragment == null) {
                        dbFragment = DbFragment.newInstance("2", "");
                    }
                    return dbFragment;
                case 2:
                    if (bitmapFragment == null) {
                        bitmapFragment = BitmapFragment.newInstance("3", "");
                    }
                    return bitmapFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "网络";
                case 1:
                    return "数据库";
                case 2:
                    return "图片";
            }
            return null;
        }
    }

}
